//
//  ShoukuanViewController.m
//  JimiPay
//
//  Created by yull on 16/1/7.
//  Copyright © 2016年 com.shengyun. All rights reserved.
//

#import "ShoukuanViewController.h"
#import "UIButton+Util.h"
#import "FukuanWebViewController.h"
#import "WXshoukuanViewController.h"

#import "JMGTMBase64.h"

@interface ShoukuanViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *categories;
@property (nonatomic, strong) UIView *downListV;
@property (nonatomic, strong) UITableView *downListTV;

@property (nonatomic, assign) double money;
@property (nonatomic, assign) long long pointMoney;
@property (nonatomic, assign) BOOL hasPoint;
@property (nonatomic, strong) NSString *category;

@end

@implementation ShoukuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"交易收款";
    [self setupCustomBackItem];
    
    self.category = @"00";
    self.categoryL.text = @"百货商品";
    
    [self.wxShoukuanB centerImageAndTitle:2];
    [self.shoukuanB centerImageAndTitle:2];
}

- (void)initDownListView {
    
    self.categories = @[@"百货商品",
                        @"航空公司",
                        @"大型景区售票"];
    
    UIView *downListV = [[UIView alloc] initWithFrame:self.view.bounds];
    downListV.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    downListV.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.downListV = downListV;
    
    UITableView *downListTV = [[UITableView alloc] initWithFrame:CGRectMake(8, 3, self.view.frame.size.width - 16, 132) style:UITableViewStylePlain];
    downListTV.delegate = self;
    downListTV.dataSource = self;
    downListTV.layer.cornerRadius = 5;
    downListTV.layer.masksToBounds = YES;
    downListTV.bounces = NO;
    downListTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [downListTV registerClass:[UITableViewCell class] forCellReuseIdentifier:@"DownListCell"];
    self.downListTV = downListTV;
    [downListV addSubview:downListTV];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideDownList)];
    tapGesture.delegate = (id<UIGestureRecognizerDelegate>)self;
    [downListV addGestureRecognizer:tapGesture];
    
    downListV.hidden = YES;
    CGRect frame = downListV.frame;
    frame.origin.y = CGRectGetMaxY(self.categoryL.superview.frame);
    downListV.frame= frame;
    [self.view addSubview:downListV];
}

- (void)setMoney:(double)money
{
    _money = money;
    
    self.moneyL.text = [NSString stringWithFormat:@"%.2f", _money];
}

- (void)hideDownList {
    
    self.downListV.backgroundColor = [UIColor clearColor];
    [UIView animateWithDuration:0.3f animations:^{
        self.downListTV.transform = CGAffineTransformMakeScale(0, 0);
        self.downListV.hidden = YES;
    } completion:^(BOOL finished) {
        
    }];

}

- (IBAction)button_downList:(id)sender {
    
    if (!self.categories) {
        [self initDownListView];
    }
    
    if (self.downListV.hidden) {
        self.downListV.hidden = NO;
        self.downListV.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        self.downListTV.transform = CGAffineTransformMakeScale(0, 0);
        [UIView animateWithDuration:0.3f animations:^{
            self.downListTV.transform = CGAffineTransformMakeScale(1, 1);
        }];
    } else {
        [self hideDownList];
    }
}

- (IBAction)button_weixinShoukuan:(id)sender {
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [TDHttpEngine requestFor_PARORDER_WithCustId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID]
                                      custMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE]
                                      prdordType:self.category
                                       prdordAmt:[@(self.money) stringValue]
                                         payType:@"1"
                                        complete:^(BOOL succeed, NSString *msg, id reqBody) {
                                            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                                            if (succeed) {
                                                NSString *url = [reqBody objectForKey:@"url"];
                                                if ([url isKindOfClass:[NSString class]] && url.length) {
                                                    WXshoukuanViewController *controller = [[WXshoukuanViewController alloc] init];
                                                    controller.iconData = [JMGTMBase64 decodeString:url];
                                                    controller.money = [NSString stringWithFormat:@"%.2f", self.money];
                                                    
                                                    controller.hidesBottomBarWhenPushed = YES;
                                                    
                                                    NSMutableArray *vcs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
                                                    [vcs removeLastObject];
                                                    [vcs addObject:controller];
                                                    [self.navigationController setViewControllers:vcs animated:YES];
                                                }
                                            } else {
                                                [self.view makeToast:msg duration:2.0f position:@"center"];
                                            }
                                        }];
}

- (IBAction)onClickNumberBtn:(UIButton *)sender {
    
    NSString *title = [sender titleForState:0];
    
    NSInteger scale = [title isEqualToString:@"00"] ? 100 : 10;
    
    if (self.hasPoint) {
        self.pointMoney = self.pointMoney * scale + [title doubleValue];
        while (self.pointMoney > 100) {
            self.pointMoney /= 10;
        }
        self.money = [[NSString stringWithFormat:@"%lld.%lld", (long long)self.money, (long long)self.pointMoney] doubleValue];
    } else {
        self.money = self.money * scale + [title doubleValue];
    }
}
- (IBAction)onClickBackBtn:(id)sender {
    
    self.money = 0.0;
    self.pointMoney = 0;
    self.hasPoint = NO;
}
- (IBAction)onClickShoukuanBtn:(id)sender {
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [TDHttpEngine requestFor_PARORDER_WithCustId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID]
                                      custMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE]
                                      prdordType:self.category
                                       prdordAmt:[@(self.money) stringValue]
                                         payType:@"0"
                                        complete:^(BOOL succeed, NSString *msg, id reqBody) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if (succeed) {
            NSString *url = [reqBody objectForKey:@"url"];
            if ([url isKindOfClass:[NSString class]] && url.length) {
                FukuanWebViewController *controller = [[FukuanWebViewController alloc] init];
                controller.url = url;
                
                controller.hidesBottomBarWhenPushed = YES;
                
                NSMutableArray *vcs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
                [vcs removeLastObject];
                [vcs addObject:controller];
                [self.navigationController setViewControllers:vcs animated:YES];
            }
        } else {
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
}
- (IBAction)onClickPointBtn:(id)sender {
    
    if (self.hasPoint) {
        return;
    }
    
    self.hasPoint = YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    if (CGRectContainsPoint(self.downListTV.frame, [touch locationInView:self.downListV])) {
        return NO;
    }
    return YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.categories.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DownListCell"];
    cell.textLabel.text = self.categories[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self hideDownList];
    
    self.categoryL.text = self.categories[indexPath.row];
    
    if (indexPath.row == 0) {
        self.category = @"00";
    } else if (indexPath.row == 1) {
        self.category = @"01";
    } else {
        self.category = @"02";
    }
}

@end
